package com.betterwood.dragon.controllers;

import com.betterwood.dragon.core.common.JsonResult;

import com.betterwood.dragon.entity.Site;
import com.betterwood.dragon.entity.dto.IdDTO;

import com.betterwood.dragon.entity.dto.SiteUpdate;

import com.betterwood.dragon.service.SiteService;
import com.betterwood.dragon.service.WormService;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/site")
@CrossOrigin
// 使用Lombok自动生成注入的构造函数
@RequiredArgsConstructor
@Slf4j
public class SiteController {

    private final SiteService siteService;
    private final WormService wormService;

    @PostMapping(value = "/updateStatus")
    public JsonResult<Site> updateStatus(@RequestBody SiteUpdate sUpdate) {
        Site p = siteService.findById(sUpdate.getId());
        p.setStatus(sUpdate.getStatus());
        return JsonResult.success(siteService.save(p));
    }

    @PostMapping(value = "/autoLogin")
    public JsonResult<Site> autoLogin(@RequestBody IdDTO idDTO) {
        wormService.autoLogin(idDTO.getId());
        return JsonResult.success(siteService.findById(idDTO.getId())); // 因为已经做了字段验证,所以几乎没有错误可能性, 直接存了。

    }

    @PostMapping(value = "/JfLogin")
    public JsonResult<Site> JfLogin(@RequestBody IdDTO idDTO) {
        wormService.JfLogin(idDTO.getId());
        return JsonResult.success(siteService.findById(idDTO.getId())); // 因为已经做了字段验证,所以几乎没有错误可能性, 直接存了。

    }

    @PostMapping(value = "/singleLogin")
    public JsonResult<Site> singleLogin(@RequestBody IdDTO idDTO) {
        wormService.singleLogin(idDTO.getId());
        return JsonResult.success(siteService.findById(idDTO.getId())); // 因为已经做了字段验证,所以几乎没有错误可能性, 直接存了。

    }
}
